import 'package:dss_base/widgets/base/base_fragment_view_model.dart';
import 'package:flutter/cupertino.dart';

import '../../dss_base.dart';

/// 一般用于需要请求数据，如果请求失败就隐藏不显示（可以重写为其他展示方式）的 widget
abstract class BaseFragmentWidgetState<T extends StatefulWidget, VM extends BaseFragmentViewModel> extends BaseState<T, VM> {

  /// 页面状态 - 初始化
  Widget onCreateInitPage(BuildContext context) => SizedBox.shrink();

  /// 页面状态 - 加载中
  Widget onCreateLoadingPage(BuildContext context) => SizedBox.shrink();

  /// 页面状态 - 加载完成后的内容
  Widget onCreateSuccessPage(BuildContext context);

  /// 页面状态 - 加载失败
  Widget onCreateFailurePage(BuildContext context) => SizedBox.shrink();

  /// 页面状态 - 空数据
  Widget onCreateEmptyPage(BuildContext context) => SizedBox.shrink();

  @override
  void initData() {
    super.initData();
    viewModel.initData();
  }

  @override
  Widget onBuild(BuildContext context) {
      return _getWidgetByState(context, viewModel.pageState);
  }

  /// 根据状态来显示当前的 Widget
  Widget _getWidgetByState(
      BuildContext context,
      PageState state) {
    Widget page;
    switch (state) {
      case PageState.initState:
        page = onCreateInitPage(context);
        break;
      case PageState.loadingState:
        page = onCreateLoadingPage(context);
        break;
      case PageState.successState:
        page = onCreateSuccessPage(context);
        break;
      case PageState.failureState:
        page = onCreateFailurePage(context);
        break;
      case PageState.emptyState:
        page = onCreateEmptyPage(context);
        break;
    }
    return page;
  }
}
